/**
 * Class csvinput
 * 
 * @author Hussein Patwa
 * @date 13 March 2008
 * @version 2.1 Generic reader for .csv files
 */

package trend;

import java.io.*;
import java.util.*;

@SuppressWarnings("all")
public class Csvinput {
  /** Constructor for csvinput object */
  String[][] values;
  int rowNum, colNum;

  public Csvinput(File file, int rows, int cols) {
    values = new String[rows][cols];

    /** Buffer reader for reading in data stream */
    try {
      BufferedReader bufRdr = new BufferedReader(new FileReader(file));

      /** Counter for each line of the file */
      String line = null;
      int row = 0;
      int col = 0;

      /** Iterate through each line of the file */
      while ((line = bufRdr.readLine()) != null) {
        col = 0;
        StringTokenizer st = new StringTokenizer(line, ",");
        while (st.hasMoreTokens()) {
          /** get next token and store it in the array */
          values[row][col] = st.nextToken();
          col++;
        }
        row++;
      }
      rowNum = row;
      colNum = col;

      /** close the file */
      bufRdr.close();
    } catch (IOException ex) {
    }
  }

  /**
   * Get values read from file that are stored in the array *
   * 
   * @return string
   */
  public String[][] getValues() {
    return values;
  }

  /**
   * Get the number of rows in the file (corresponds to observations)
   * 
   * @return int
   */
  public int getNumberOfRows() {
    return rowNum;
  }

  /**
   * Get number of colums read into array
   * 
   * @return int
   */
  public int getNumberOfColumns() {
    return colNum;
  }

  /**
   * Main method for the csvinput class
   * 
   * @param args Arguements to the method
   * @return null
   */
  public static void main(String[] args) {
    Csvinput inObj = new Csvinput(new File("data.csv"), 50, 6);
    System.out.println(inObj.values.length);
    for (int i = 0; i < 5; i++)
      for (int j = 0; j < 5; j++)
        System.out.println(inObj.values[i][j]);
  }
}
